/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.hagopa.games.moneypool.utils;

import java.util.Iterator;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.hagopa.games.moneypool.dblayer.DBCustomLogger;

/**
 *
 * @author IGG
 */
public class CommonTracer implements Runnable{
    private int maxQsize = 128;
    private ConcurrentLinkedQueue<LogRecord> msgQ;
    private ICustLogger loggerHandler = null;
    private static CommonTracer instance = null;
    private boolean runFlag = false;
    private boolean LogEnabled = false;

    public CommonTracer()
    {
        msgQ = new ConcurrentLinkedQueue<LogRecord>();
        
        this.runFlag = true;
        System.out.println("CommonTracer created");
        new Thread( this ).start();
    }
    public void setEnableFlag(boolean flag)
    {
        LogEnabled = flag;
    }
    public static CommonTracer getInstance()
    {
        if(instance == null)
	{
		instance = new CommonTracer();

 	}
        return instance;
    }
    public void setLoggerType(int type)
    {
        if(type == 1 )
        {
            this.loggerHandler =  new DBCustomLogger();
        }
    }
    public boolean setCustomLogger(ICustLogger logger)
    {
        if(logger == null)
            return false;
        this.loggerHandler = logger;
        return true;
    }
    public void setmaxQsize(int maxQsz)
    {
        this.maxQsize = maxQsz;
    }
    public int getmaxQsize()
    {
        return this.maxQsize;
    }
    public void storeLogRec(LogRecord newRec)
    {
        if(LogEnabled && this.msgQ.size()< this.maxQsize)
            msgQ.add(newRec);

    }
    public void storeLogmsg(int servId, long gid, long cid, int msg_type, String actionmsg)
    {
        //TODO action_type should explain if write it to game_log or user_log
        if(actionmsg != null || actionmsg.length() >1)
        {
            LogRecord tmprec = new LogRecord(servId, gid, cid, msg_type, actionmsg);
            if(LogEnabled && this.msgQ.size()< this.maxQsize)
            {
                msgQ.add(tmprec);
                System.out.println("[CommonTracer](storeLogmsg) stored new LogRecord");
            }
        }
        
    }

    public void run() {
        System.out.println("CommonTracer Thread Started");
        while(this.runFlag)
        {
            if(this.msgQ.isEmpty())
            {
                try {
                    Thread.sleep(10);
                } catch (InterruptedException ex) {
                    Logger.getLogger(CommonTracer.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
            else
            {
                System.out.println("[CommonTracer](run) Has Log Records in Q");
                
                LogRecord tmplog = (LogRecord)this.msgQ.remove();
                loggerHandler.sendLogMsg(tmplog);

               
            }
        }
    }
}
